

import java.util.Random;

public class Sys {
	private static void init() {
		// Prepare GUI container
		newFrame = new GUI();
		eventQueue = new PriorityHeap();
		randomGenerator = new Random();
		
		// Initialize a new game
		newGame();
		
		newFrame.setVisible(true);
	}
	
	// Reset and fill the priority queue
	// Set beginning status
	// Show the initial screen
	private static void newGame() {
		// Empty the queue
		while (!eventQueue.isEmpty()) {
			eventQueue.dequeue();
		}
		
		// Populate the queue with random events
		while (eventQueue.size() < 10) {
			eventQueue.enqueue(randomGenerator.nextInt(50) + 1, randomGenerator.nextInt(5));
		}
		
		// Set state and begin
		choicesMade = 0;
		currentStatus = flowStatus.BEGINNING;
		newFrame.backStoryScreen();
	}
	
	// Determine whether the game meets the criteria to be finished
	private static boolean gameOver() {
		if (eventQueue.isEmpty() || choicesMade >= maxChoices) {
			return true;
		} else {
			return false;
		}
	}
	
	private static int handleResponse(int seedUpper) {
		if (newFrame.getChoice() == GUI.goodCommand) {
			return seedUpper | 1;
		} else if (newFrame.getChoice() == GUI.mehCommand) {
			// 50/50 chance to generate a new event
			if (randomGenerator.nextBoolean()) {
				return (seedUpper | 2);
			}
		}
		
		// If we're still in this function, add a new random event
		eventQueue.enqueue(randomGenerator.nextInt(50) + 1, randomGenerator.nextInt(5));
		return (seedUpper | 3);
	}
	
	// Responds to button clicks in the GUI
	protected static void eventNotify(String command) {
		System.out.print("Notified of " + command + " with choice of " + newFrame.getChoice() + '\n');
		if (command == GUI.nextCommand) {
			switch (currentStatus) {
				// User has picked next from the backstory screen
				// Set state to deciding reading from first in queue
				// Activate oil spill screen
				case BEGINNING:
					currentStatus = flowStatus.DECIDING;
					newFrame.stopOilSpillScreen(eventQueue.currentInfo());
					break;
				// User has picked next from the oil spill screen
				// Set state to responding
				// Increment choices
				// Pop queue
				// Handle response to choice
				// Activate educate user screen
				case DECIDING:
					++choicesMade;
					currentStatus = flowStatus.RESPONDING;
					int thisResponse = eventQueue.currentInfo() << 2;
					eventQueue.dequeue();
					newFrame.educateUserScreen(handleResponse(thisResponse));
					break;
				// User has picked next from the educate user screen
				// Set state based on game over criteria
				// Show next question screen or game over screen
				case RESPONDING:
					if (gameOver()) {
						currentStatus = flowStatus.ENDING;
						newGame();
					} else {
						currentStatus = flowStatus.DECIDING;
						newFrame.stopOilSpillScreen(eventQueue.currentInfo());
					}
					break;
				case ENDING:
			}
		} else if (command == GUI.endCommand) {
			
		}
	}
	
	protected static enum flowStatus {
		BEGINNING, DECIDING, RESPONDING, ENDING
	}
	
	protected static final int maxChoices = 10;
	
	protected static flowStatus currentStatus;
	protected static int choicesMade;
	
	private static Random randomGenerator;
	
	private static PriorityHeap eventQueue;
	private static GUI newFrame;
	
	// Application entry point
	public static void main(String args[]) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                init();
            }
        });
    }
}
